package com.ruoyi.project.cms.sm.depart.service;

import com.ruoyi.common.constant.CMSConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.sm.depart.domain.Depart;
import com.ruoyi.project.cms.sm.depart.mapper.DepartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：MrL
 * 时间：2020-05-17
 */
@Service
public class DepartServiceImpl implements IDepartService {

    @Autowired
    private DepartMapper departMapper;

    /**
     * 根据搜索条件分页查询部门数据
     * @param depart
     * @return
     */
    @Override
    public List<Depart> selectDepartList(Depart depart) {
        return departMapper.selectDepartList(depart);
    }

    /**
     * 校验部门名称是否唯一
     * @param depart
     * @return
     */
    @Override
    public String checkDepartNameUnique(Depart depart) {
        Long deptId = StringUtils.isNull(depart.getDeptId()) ? -1L : depart.getDeptId();
        Depart info = departMapper.checkDepartNameUnique(depart.getDeptName());
        if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue()) {
            return CMSConstants.DEPART_NAME_NOT_UNIQUE;
        }
        return CMSConstants.DEPART_NAME_UNIQUE;
    }

    /**
     * 添加部门
     * @param depart
     * @return
     */
    @Override
    public int insertDepart(Depart depart) {
        return departMapper.insertDepart(depart);
    }

    /**
     * 批量删除部门
     * @param ids
     * @return
     */
    @Override
    public int deleteDepartByIds(String ids) {
        Long[] deptIds = Convert.toLongArray(ids);
        return departMapper.deleteDepartByIds(deptIds);
    }

    /**
     * 根据部门ID查询部门信息
     * @param deptId
     * @return
     */
    @Override
    public Depart selectDepartById(Long deptId) {
        return departMapper.selectDepartById(deptId);
    }

    /**
     * 更新部门信息
     * @param depart
     * @return
     */
    @Override
    public int updateDepart(Depart depart) {
        return departMapper.updateDepart(depart);
    }
}
